package com.nulldev.util.web.YouTubeExtractor.subtitles;

import java.util.List;
import com.nulldev.util.JVM.Arguments;
import com.nulldev.util.internal.backport.concurrency9.Lists;
import com.nulldev.util.logging.Logger;
import com.nulldev.util.logging.LoggerFactory;
import com.nulldev.util.manage.Version;
import com.nulldev.util.web.YouTubeExtractor.subtitles.types.JSONSubtitleParser;
import com.nulldev.util.web.YouTubeExtractor.subtitles.types.SRV3SubtitleParser;
import com.nulldev.util.web.YouTubeExtractor.subtitles.types.TTMLSubtitleParser;

public class SubtitleParser {
	private static final Logger log = LoggerFactory.getLoggerD(Arguments.hasArgument("--Subtitles.debug"));

	/*
	 * TODO: - Add support for ruby text. - Try to support etEdgeType.
	 */

	public static final Version VERSION = new Version("1.0.4");
	public static final boolean USE_NEW_LOGIC = true;
	public static final boolean ENABLE_PEN_FORMATTING = Arguments.notArguments("--SubtitleParser.disablePenFormatting");
	public static final boolean ENABLE_BR_HACK = Arguments.hasArgument("--Subtitles.brHack");

	public static enum FMTVersion {
		UNKNOWN(""),
		JSON3("json3"),
		SRV1("srv1"),
		SRV2("srv2"),
		SRV3("srv3"),
		TTML("ttml"),
		VTT("vtt"),

		;

		public final String format;

		private FMTVersion(final String format) {
			this.format = format;
		}

		public static FMTVersion get(final String val) {
			for (final FMTVersion i : values())
				if (i.format.equalsIgnoreCase(val))
					return i;
			return UNKNOWN;
		}
	}

	public static List<Subtitle> getSubtitlesF(final String data, final FMTVersion version) {
		return getSubtitlesF(data, true, version);
	}

	public static List<Subtitle> getSubtitlesF(final String data, final boolean allowStyling, final FMTVersion version) {
		switch (version) {
			case JSON3:
				return JSONSubtitleParser.getSubtitlesJSON(data, allowStyling, version);
			case TTML:
				return TTMLSubtitleParser.getSubtitles(data);
			case SRV3:
				return SRV3SubtitleParser.getSubtitles(data);
			case UNKNOWN:
			default: {
				log.warn("Unsupported format: " + version);
				return Lists.of();
			}
		}
	}

}
